const initMenu = (router, menu) => {
    // if (menu.length === 0) return

    let menus = formatRoutes(menu)

    let unfound = { path: '*', redirect: '/404', hidden: true }

    menus.push(unfound)

    router.addRoutes(menus)
    window._menus = menus
    window.sessionStorage.setItem('_menus', JSON.stringify(menu))
}

const formatRoutes = (aMenu) => {
    const aRouter = []
    aMenu.forEach(oMenu => {
        const {
            path,
            component,
            name,
            childrens,
            meta
        } = oMenu
        if (!validatenull(component)) {
            const oRouter = {
                path: path,
                component(resolve) {
                    let componentPath = component

                    require([`@pages/index/views/${componentPath}.vue`], resolve)
                },
                name: name,
                children: validatenull(childrens) ? [] : formatRoutes(childrens),
                meta: meta
            }
            aRouter.push(oRouter)
        }

    })
    return aRouter
}

const validatenull = (val) => {
    return val == null || val == '' || val == void (0)
}

export default initMenu;
